package com.novabluetooth.services.ktxyj;

import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Vector;

import com.contec.jar.contec08a.DeviceCommand;
import com.contec.jar.contec08a.DevicePackManager;
import com.health.viewscontrols.NovaApplication;
import com.novabluetooth.data.GlobalContsBlueThooth;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MtBuf {
	private static final String TAG = "com.testBlueTooth.Mtbuf";
	public static Vector<Integer> m_buf = null;

	public static final int e_pack_pressure_back = 0x46;

	private Context ctx;

	public MtBuf(Context ctx) {
		m_buf = new Vector<Integer>();
		this.ctx = ctx;
	}

	public MtBuf() {
		m_buf = new Vector<Integer>();
	}

	public synchronized int Count() {
		return m_buf.size();
	}

	DevicePackManager mPackManager = new DevicePackManager();
//	private DeviceData mDeviceData;
	public static final int e_pack_hand_back = 0x48;
	public static final int e_pack_oxygen_back = 0x47;
	private int mType = 0;
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public synchronized void write(byte[] buf, int count, final OutputStream pOutputStream) throws Exception {

		int state = mPackManager.arrangeMessage(buf, count, mType);
		System.out.println(state + "-------------------");
		int x = DevicePackManager.Flag_User;
		switch (state) {
		case e_pack_hand_back:
			switch (mType) {
			case 9:
				mType = 5;
				pOutputStream.write(DeviceCommand.DELETE_BP());
				break;
			case 0:
				mType = 3;
				pOutputStream.write(DeviceCommand.correct_time_notice);
				break;
			case 3:
				mType = 1;

				if (x == 0x11) {
					mType = 7;// 三个用户
				} else {
					mType = 1;// 单用户
				}
				if(NovaApplication.isXueYa){
					pOutputStream.write(DeviceCommand.REQUEST_BLOOD_PRESSURE());
				}else{
					pOutputStream.write(DeviceCommand.REQUEST_OXYGEN());
				}
				break;
			}
			break;
		case 0x30:// 确认校正时间正确
			pOutputStream.write(DeviceCommand.Correct_Time());
			break;
		case 0x40:// 校正时间正确
			pOutputStream.write(DeviceCommand.REQUEST_HANDSHAKE());
			break;
		case e_pack_pressure_back:
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {//// 防止最后一条命令血压设备接收不到
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			ArrayList<byte[]> _dataList = mPackManager.mDeviceData.mData_blood;
			int _size = _dataList.size();
			if (_size > 0) {
				// pOutputStream.write(DeviceCommand.REPLAY_CONTEC08A());

				Log.e(TAG, "-------Pressure---血压条数----" + _size);

				for (int i = 0; i < _size; i++) {
					byte[] _byte = _dataList.get(i);
					// DeviceData _mData = new DeviceData(_byte);
					Log.e(TAG, "-------Pressure-" + Arrays.toString(_byte));
					if (_byte.length > 0) {

						DeviceData deviceData = new DeviceData();
						int lowPre = _byte[2] & 0xff; // 低压
						int hiPre = (_byte[0] << 8 | _byte[1]) & 0xff;
						int ml = _byte[3] & 0xff; // 脉率
						int pjy = _byte[4] & 0xff; // 平均压
						int year = _byte[5] & 0xff; // 年
						int month = _byte[6] & 0xff; // 月
						int day = _byte[7] & 0xff; // 日
						int hours = _byte[8] & 0xff; // 时
						int min = _byte[9] & 0xff; // 分
						int seconds = _byte[10] & 0xff; // 秒

						deviceData.setM_hiPre(hiPre);
						deviceData.setM_lowPre(lowPre);
						deviceData.setM_ml(ml);
						deviceData.setM_pjy(pjy);
						deviceData.setmSaveDate(new Date(100 + year, month - 1, day, hours, min, seconds));
						DeviceData.m_deviceDataList.add(deviceData);

						Log.i("com.testbluetooth", "测试时间：" + dateFormat.format(deviceData.getmSaveDate()));
						Log.i("com.testbluetooth", "数据：高压＝" + deviceData.getM_hiPre() + "低压＝" + deviceData.getM_lowPre()
								+ "平均压＝" + deviceData.getM_pjy() + "脉率=" + deviceData.getM_ml());
					}
					
				}
				System.out.println("DeviceData.m_deviceDataList*********************"+DeviceData.m_deviceDataList.size());
				final Intent intent = new Intent(BluetoothChatService.ACTION_DATA_AVAILABLE);
				ctx.sendBroadcast(intent);

			} else {
				Log.e(TAG, "-------Pressure---没有数据----");
				
				GlobalContsBlueThooth.showToast(ctx, "无血压数据");
			}

			break;
		case e_pack_oxygen_back:
			System.out.println("这里是血氧");
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {//// 防止最后一条命令血压设备接收不到
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			ArrayList<byte[]> _dataList_Xueyang = mPackManager.mDeviceData.mData_oxygen;
			System.out.println(_dataList_Xueyang.size() + "**************");
			int _size_Xueyang = _dataList_Xueyang.size();
			if (_size_Xueyang > 0) {
				for (int i = 0; i < _size_Xueyang; i++) {
					byte[] _byte_Xueyang = _dataList_Xueyang.get(i);
					Log.e(TAG, "-------Pressure-" + Arrays.toString(_byte_Xueyang));
					DeviceDataXueYang deviceDataXueYang=new DeviceDataXueYang();
					int xueYang=_byte_Xueyang[0] & 0xFF;//血氧
					int maiLv=_byte_Xueyang[1] & 0xFF;//脉率
					int year=_byte_Xueyang[2] & 0xFF;//年
					int month=_byte_Xueyang[3] & 0xFF;//月
					int day=_byte_Xueyang[4] & 0xFF;//日
					int hour=_byte_Xueyang[5] & 0xFF;//时
					int min=_byte_Xueyang[6] & 0xFF;//分
					int seconds=_byte_Xueyang[7] & 0xFF;//秒
					System.out.println("血氧：" + (_byte_Xueyang[0] & 0xFF) + ",脉率：" + (_byte_Xueyang[1] & 0xFF) + ";"
							+ (_byte_Xueyang[2] & 0xFF) + "年" + (_byte_Xueyang[3] & 0xFF) + "月"
							+ (_byte_Xueyang[4] & 0xFF) + "日" + (_byte_Xueyang[5] & 0xFF) + "时"
							+ (_byte_Xueyang[6] & 0xFF) + "分" + (_byte_Xueyang[7] & 0xFF) + "秒");
					deviceDataXueYang.setXueYang(xueYang);
					deviceDataXueYang.setMaiLv(maiLv);
					deviceDataXueYang.setmSaveDate(new Date(100 + year, month - 1, day, hour, min, seconds));
					DeviceDataXueYang.m_deviceDataList_XueYang.add(deviceDataXueYang);

				}
				final Intent intent = new Intent(BluetoothChatService.ACTION_DATA_AVAILABLE);
				ctx.sendBroadcast(intent);
				
				
			} else {
				GlobalContsBlueThooth.showToast(ctx, "无血氧数据");
			}
			break;
		}

	}

}
